import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddGoogleAuthFields1703000000001 implements MigrationInterface {
  name = 'AddGoogleAuthFields1703000000001';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Add Google authentication fields to users table
    await queryRunner.query(`
      ALTER TABLE \`users\` 
      ADD COLUMN \`googleId\` varchar(255) NULL UNIQUE,
      ADD COLUMN \`picture\` varchar(500) NULL,
      ADD COLUMN \`provider\` enum('local', 'google') NOT NULL DEFAULT 'local',
      MODIFY COLUMN \`username\` varchar(255) NULL,
      MODIFY COLUMN \`password\` varchar(255) NULL
    `);

    // Add index for Google ID
    await queryRunner.query(`
      CREATE INDEX \`IDX_googleId\` ON \`users\` (\`googleId\`)
    `);

    // Add index for provider
    await queryRunner.query(`
      CREATE INDEX \`IDX_provider\` ON \`users\` (\`provider\`)
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Drop indexes
    await queryRunner.query(`DROP INDEX \`IDX_provider\` ON \`users\``);
    await queryRunner.query(`DROP INDEX \`IDX_googleId\` ON \`users\``);

    // Remove Google authentication fields
    await queryRunner.query(`
      ALTER TABLE \`users\` 
      DROP COLUMN \`provider\`,
      DROP COLUMN \`picture\`,
      DROP COLUMN \`googleId\`,
      MODIFY COLUMN \`password\` varchar(255) NOT NULL,
      MODIFY COLUMN \`username\` varchar(255) NOT NULL
    `);
  }
}